import { _decorator, Component, Mask, Node, size, tween, Tween, UIOpacity, UITransform, v3, Widget } from 'cc';
import $planet_GameAssets_poof$ from '../manager/game-assets';
const { ccclass, property } = _decorator;

@ccclass('$planet_Guide_poof$')
export class $planet_Guide_poof$ extends Component {
	private static $planet__ins_poof$: $planet_Guide_poof$;
	/** 全局唯一安全实例 */
	public static get $planet_ins_poof$(){
		return $planet_Guide_poof$.$planet__ins_poof$;
	}

	@property(Mask)
	private $planet_mask_poof$: Mask = null;
	@property(UITransform)
	private $planet_ui_content_poof$: UITransform = null;

	private $planet_ui_poof$: UITransform;
	private $planet_ui_mask_poof$: UITransform;
	private $planet_npromise_poof$: abd.$planet_NudityPromise_poof$<0>;

	protected onLoad(): void {
		$planet_Guide_poof$.$planet__ins_poof$ = this;
		this.$planet_ui_poof$ = this.node.getComponent(UITransform);
		this.$planet_ui_mask_poof$ = this.$planet_mask_poof$.getComponent(UITransform);
		this.$planet_ui_mask_poof$.setContentSize(0, 0);
		this.$planet_mask_poof$.node.setPosition(0, 0);
		this.$planet_ui_content_poof$.node.setPosition(0, 0);
		this.node.active = false;

		this.node.getChildByName('toucher').on(Node.EventType.TOUCH_END, this.$planet_touchEnd_poof$, this);

		abd.$planet_cc_poof$.$planet_skinPeeler_poof$(this.$planet_ui_content_poof$.node, $planet_GameAssets_poof$.$planet_getLangTexture_poof$(5), {width: true});
	}

	private $planet_touchEnd_poof$(){
		if(this.$planet_npromise_poof$) this.$planet_npromise_poof$.$planet_resolve_poof$(0);
	}

	public static $planet_showGroup_poof$(list: Node[]){
		let ss = list.reduce((ls, node)=>{
			let ui = node.getComponent(UITransform);
			let pos = node.worldPosition;
			let t = pos.y + ui.height * (1-ui.anchorY);
			let b = pos.y - ui.height * ui.anchorY;
			let l = pos.x - ui.width * ui.anchorX;
			let r = pos.x + ui.width * (1-ui.anchorX);
			ls[0].push(t);
			ls[1].push(b);
			ls[2].push(l);
			ls[3].push(r);
			return ls;
		}, [[], [], [], []] as [number[], number[], number[], number[]]);

		let top = Math.max(...ss[0]);
		let bottom = Math.min(...ss[1]);
		let left = Math.min(...ss[2]);
		let right = Math.max(...ss[3]);

		let that = $planet_Guide_poof$.$planet_ins_poof$;
		that.$planet_npromise_poof$ = new abd.$planet_NudityPromise_poof$<0>();
		let ui_shade = that.$planet_mask_poof$.node.getChildByName('shade').getComponent(UITransform);

		let mask_size = size(right-left, top-bottom);
		let target_pos = v3();
		let content_pos = v3();
		that.$planet_ui_poof$.convertToNodeSpaceAR(v3(left + mask_size.width * that.$planet_ui_mask_poof$.anchorX, bottom + mask_size.height * that.$planet_ui_mask_poof$.anchorY, 0), target_pos);
		that.$planet_ui_poof$.convertToNodeSpaceAR(v3(0, top + that.$planet_ui_content_poof$.height * that.$planet_ui_content_poof$.anchorY), content_pos);
		content_pos.x = 0;

		[that.$planet_mask_poof$.node, that.$planet_ui_content_poof$.node, ui_shade.node].forEach(n=>Tween.stopAllByTarget(n));
		[that.$planet_ui_mask_poof$, ui_shade].forEach(ui=>Tween.stopAllByTarget(ui));
		Tween.stopAllByTarget(ui_shade.getComponent(UIOpacity));
		that.$planet_ui_content_poof$.node.setPosition(abd.$planet_cc_poof$.$planet_VecAssist_poof$.$planet_getPosition_poof$(that.$planet_ui_content_poof$.node, '$planet_center_poof$', '$planet_over_bottom_poof$'));
		that.node.active = true;
		that.$planet_ui_poof$.getComponent(Widget).updateAlignment();
		ui_shade.setContentSize(that.$planet_ui_poof$.contentSize);

		let duration = 0.5;

		tween(that.$planet_mask_poof$.node) .to(duration, {position: target_pos}, {easing: 'quartInOut'}) .start();
		tween(that.$planet_ui_mask_poof$) .to(duration, {contentSize: mask_size}, {easing: 'quartInOut'}) .start();
		tween(ui_shade.node) .to(duration, {position: v3(-target_pos.x, -target_pos.y)}, {easing: 'quartInOut'}) .start();
		tween(ui_shade.getComponent(UIOpacity)) .set({opacity: 0}) .to(duration, {opacity: 120}, {easing: 'quartInOut'}) .start();
		tween(that.$planet_ui_content_poof$.node) .to(duration, {position: content_pos}, {easing: 'quartInOut'}) .start();

		return that.$planet_npromise_poof$.$planet_promise_poof$;
	}
}

